package com.bk.game;

import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.bk.game.global.BkGlobal;
import com.bk.game.screen.AdScreen;

public class BKGame extends AndroidApplication {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
		// initialize(new FirstGame(), false);
		AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
		config.useAccelerometer = false;
		config.useCompass = false;
		config.useWakelock = true;
		config.useGL20 = true;
		ApplicationGame game = new ApplicationGame(new AdScreen());
		BkGlobal.currentGame = game;
		DisplayMetrics dm2 = getResources().getDisplayMetrics();
		BkGlobal.factorx = dm2.widthPixels / 640f;
		BkGlobal.factory = BkGlobal.factorx;
		LinearLayout layout = new LinearLayout(this);
		layout.setOrientation(LinearLayout.VERTICAL);
		LinearLayout adLayout = (LinearLayout) this.getLayoutInflater().inflate(R.layout.ad, null);
		View gameView = initializeForView(game, config);
		LinearLayout.LayoutParams adParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		layout.addView(gameView, new LinearLayout.LayoutParams(dm2.widthPixels, (int) (BkGlobal.factorx * 920f)));
		layout.addView(adLayout, adParams);
		setContentView(layout);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

		// BkGlobal.currentGame.dispose();
		BkGlobal.getInstance().dispose();
		BkGlobal.gameScreen = null;
		BkGlobal.mainScreen = null;
		BkGlobal.currentGame = null;
		BkGlobal.actors.clear();
		BkGlobal.flyActors.clear();
		if (BkGlobal.manager != null) {
			BkGlobal.manager.clear();
			BkGlobal.manager.dispose();
			BkGlobal.manager = null;
		}
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
	}
}